//<editor-fold defaultstate="collapsed" desc=" Header ">
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package webproxy;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author MinhVu
 */
//</editor-fold>
public class Port_Thread  extends Thread
{   
    private Boolean flag_State ;
    private ServerSocket welcomeSocket;
    private Socket s;
    private int port;
    private Config config;
    private ArrayList<ACL_BlackList> listDomain;
    private ArrayList<ACL_BlackList> listIP;
    
    
    //<editor-fold defaultstate="collapsed" desc="Get - Set">
    public ArrayList<ACL_BlackList> getListDomain()
    {
        return listDomain;
    }
    public ArrayList<ACL_BlackList> getListIP()
    {
        return listIP;
    }
    public Config getConfig()
    {
        return config;
    }

    public int getPort()
    {
        return port;
    }
    public Boolean getFlag_State()
    {
        return flag_State;
    }
    public void setListDomain(ArrayList<ACL_BlackList> ls)
    {
        listDomain = ls;
    }
    public void setListIP(ArrayList<ACL_BlackList> ls)
    {
        listIP = ls;
    }
    public void setConfig(Config cof)
    {
        config = cof;
    }
    public void setFlag_State(Boolean f)
    {
        flag_State = f;
    }
    //</editor-fold>
    
    public  Port_Thread(int p,Config cof,ArrayList<ACL_BlackList> lsDomain,ArrayList<ACL_BlackList> lsIP)
    {
        port = p;
        this.setName("Port : "+port);
        listDomain = lsDomain;
        listIP = lsIP;
        flag_State = false;
        config =cof;
    }
    
    /**
     * @param args the command line arguments
     */
    @Override
    public void run() {
        try {
           
            welcomeSocket = new ServerSocket(port); //Tao port sever
            System.out.println("Server Start");
            Request_Thread x;
            int k =1;
            String threadName;
            while (flag_State)
            {
                s = welcomeSocket.accept();
                if(!flag_State)
                    break;
                //Tao mot thread moi voi socket duoc ket noi toi 1 client da duoc accept
                threadName = "Thread - "+k;
                x = new Request_Thread(s,threadName,config,listDomain,listIP);
                x.start();
                System.out.println("Thread " + k +" OK");
                k++;
            }
            System.out.println("End Proxy");
            welcomeSocket.close();
        } catch (IOException ex) {
            Logger.getLogger(Port_Thread.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
